home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 0411.ZIP / ENCRYPT.C < prev    next >
Text File  |  1985-09-18  |  4KB  |  133 lines

  1. #include "stdio.h"
  2. main(argc,argv)
  3.     int argc;
  4.     char *argv[];
  5.     {
  6.     int  c,
  7.      i,
  8.      infile,
  9.      outfile,
  10.      passlen;
  11.     char password[9],
  12.      passchek[9];
  13.     if (argc == 1)
  14.     {
  15.     exit(0);
  16.     }
  17.     if (argv[1][0] == '#')
  18.     {
  19.     puts("\n");
  20.     puts("Program: ENCRYPT\n");
  21.     puts("Author : Peter Townsend\n");
  22.     puts("Date   : 18Sep85\n");
  23.     puts("Time   : 12:27\n");
  24.     puts("Version: 1.1\n");
  25.     exit(0);
  26.     }
  27.     if (argv[1][0] == '?')
  28.     {
  29.     puts("\n");
  30.     puts("This program reads a file and, character by character, adds a\n");
  31.     puts("key word (modulo 256) to the character.\n");
  32.     puts("\n");
  33.     puts("There are 3 parameters on the command line:\n");
  34.     puts("  1. Input filename  - the name of the file which is to be\n");
  35.     puts("                       (de-)encrypted.\n");
  36.     puts("  2. Output filename - the name of the file into which the\n");
  37.     puts("                       (de-)encrypted data is to be placed.\n");
  38.     puts("  3. Encrypt switch  - literal, either /ON or /OFF indicating\n");
  39.     puts("                       whether, respectively, the data is to\n");
  40.     puts("                       be encrypted or de-encrypted.\n");
  41.     puts("\n");
  42.     puts("Syntax: ENCRYPT [input filename] [output filename] /on|/off \n");
  43.     puts("\n");
  44.     puts("Example:  ENCRYPT coded.dbf myfile.dbf /on\n");
  45.     puts("          will ask for a password which it will use to create\n");
  46.     puts("          the coded file coded.dbf from myfile.dbf.\n");
  47.     puts("Example:  ENCRYPT coded.dbf myfile.dbf /off\n");
  48.     puts("          will ask for a password which it will use to create\n");
  49.     puts("          an uncoded file myfile.dbf from coded.dbf.\n");
  50.     puts("\n");
  51.     puts("NB If more than 1 person encrypts a file, each using their\n");
  52.     puts("   own password, then the result is not legible to either with-\n");
  53.     puts("   out the other applying their password.");
  54.     exit(0);
  55.     }
  56.     if (argc == 2)
  57.     {
  58.     printf("\nMissing Output Filename\n");
  59.     exit(1);
  60.     }
  61.     if (argc == 3)
  62.     {
  63.     printf("\nMissing Encrypt switch\n");
  64.     exit(1);
  65.     }
  66.     if (strcmp(argv[1],argv[2]) == 0)
  67.     {
  68.     printf("\nInput filename and output filename cannot be the same.\n");
  69.     exit(1);
  70.     }
  71.     for(i=0;i<strlen(argv[3]);i++)
  72.     {
  73.     argv[3][i] = toupper(argv[3][i]);
  74.     }
  75.     if (strcmp(argv[3],"/ON") != 0)
  76.     {
  77.     if (strcmp(argv[3],"/OFF") != 0)
  78.         {
  79.         printf("\nInvalid encrypt switch ---> %s\n",argv[3]);
  80.         exit(1);
  81.         }
  82.     }
  83.     if ((infile = open(argv[1],0)) == -1)
  84.     {
  85.     printf("\nCannot open input file ---> %s\n",argv[1]);
  86.     exit(1);
  87.     }
  88.     if ((outfile = open(argv[2],1)) == -1)
  89.     {
  90.     if ((outfile = creat(argv[2])) == -1)
  91.         {
  92.         printf("\nCannot open/create output file ---> %s\n",argv[2]);
  93.         exit(1);
  94.         }
  95.     }
  96.     printf("\nPlease enter your password ===> ");
  97.     scanf("%8s",password);
  98.     for(i=0;i<24;i++)
  99.     {
  100.     puts("\n");
  101.     }
  102.     printf("\nPlease re-enter your password ===> ");
  103.     scanf("%8s",passchek);
  104.     for(i=0;i<24;i++)
  105.     {
  106.     puts("\n");
  107.     }
  108.     if ((i = strcmp(passchek,password)) != 0)
  109.     {
  110.     printf("\nPasswords do not match ... ENCRYPT terminating.\n");
  111.     exit(1);
  112.     }
  113.     passlen = strlen(password);
  114.     if (strcmp(argv[3],"/OFF") == 0)
  115.     {
  116.     for(i=0;i<passlen;i++)
  117.         {
  118.         password[i] = 256 - password[i];
  119.         }
  120.     }
  121.     i = passlen;
  122.     printf("\nEncrypting ... Do NOT disturb.\n");
  123.     while ((c = fgetc(infile)) != EOF)
  124.     {
  125.     i = (++i) % passlen;
  126.     c = (c + password[i]) % 256;
  127.     putc(c,outfile);
  128.     }
  129.     printf("\nEncryption finished.\n");
  130.     fclose(infile);
  131.     fclose(outfile);
  132.     }
  133.